package com.banmao.algorithm.No1To100.No19;

/**
 *  有问题，待解决
 */
public class Solution1 {

    public ListNode removeNthFromEnd(ListNode head, int n) {

        // 虚拟头节点
        ListNode dummy_head = new ListNode(-1, head);

        // 快慢指针
        ListNode fast = dummy_head, slow = dummy_head;

        // 让快指针先走
        int fastIndex = 0;
        while(fastIndex <= n + 1 && fast != null) {
            fast = fast.next;
            fastIndex++;
        }
        if(fastIndex <= n + 1) {
            return null;
        }

        ListNode pre = slow;
        int slowIndex = 0;
        while(fast != null && fast.next != null) {
            pre = slow;
            slow = slow.next;
            fast = fast.next;
        }

        pre.next = slow.next;
        return dummy_head.next;
    }

}

class ListNode {
    int val;
    ListNode next;
    ListNode() {}
    ListNode(int val) { this.val = val; }
    ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
